package com.it.recyclerview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivityGrid extends AppCompatActivity {

    /**
     * 复用视图的控件
     */
    private RecyclerView recyclerView;

    /**
     * 显示的数据
     */
    private ArrayList<String> mDatas;

    /**
     * RecyclerView的适配器
     */
    private MyRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.找到控件
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //2.声名gridview布局方式  第二个参数代表是3列的网格视图 (垂直滑动的情况下, 如果是水平滑动就代表3行)
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        //3.给GridLayoutManager设置滑动的方向
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        //4.为recyclerView设置布局管理器
        recyclerView.setLayoutManager(gridLayoutManager);

        initData();//初始化数据
        //3.创建适配器
        adapter = new MyRecyclerViewAdapter(this, mDatas);
        //设置分割线
        recyclerView.addItemDecoration(new GridDivider(this));
        //设置添加,移除item的动画,DefaultItemAnimator为默认的
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //4.设置适配器
        recyclerView.setAdapter(adapter);
//        mItemTouchHelper.attachToRecyclerView(recyclerView);

        //添加点击事件
        adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnRecyclerItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {
                //Toast.makeText(MainActivity.this,"单击了:"+mDatas.get(position),Toast.LENGTH_SHORT).show();
                adapter.addItem(position,"添加的内容");
                Log.i("tag", "onItemClick: "+position);
                Log.i("tag", "集合: "+mDatas.toString());
            }
        });
        //设置长按事件
        adapter.setOnItemLongClickListener(new MyRecyclerViewAdapter.onRecyclerItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {
                //Toast.makeText(MainActivity.this,"长按了:"+mDatas.get(position),Toast.LENGTH_SHORT).show();
                adapter.removeItem(position);
                Log.i("tag", "onItemLongClick: "+position);
                Log.i("tag", "集合: "+mDatas.toString());
            }
        });

    }

    //初始化数据
    protected void initData(){
        mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++){
            mDatas.add("" + (char) i);
        }
    }


    ItemTouchHelper  mItemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
        /**
         * 是否处理滑动事件 以及拖拽和滑动的方向 如果是列表类型的RecyclerView的只存在UP和DOWN，
         * 如果是网格类RecyclerView则还应该多有LEFT和RIGHT
         * @param recyclerView
         * @param viewHolder
         * @return
         */
        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
                        ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                //final int swipeFlags = 0;
                final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;//代表处理滑动删除,将执行onSwiped()方法
                return makeMovementFlags(dragFlags, swipeFlags);
            } else {
                final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                //final int swipeFlags = 0;//为0 代表不处理滑动删除
                final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;//代表处理滑动删除,将执行onSwiped()方法
                return makeMovementFlags(dragFlags, swipeFlags);
            }
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            //得到当拖拽的viewHolder的Position
            int fromPosition = viewHolder.getAdapterPosition();
            //拿到当前拖拽到的item的viewHolder
            int toPosition = target.getAdapterPosition();
            if (fromPosition < toPosition) {
                for (int i = fromPosition; i < toPosition; i++) {
                    Collections.swap(mDatas, i, i + 1);//交换位置
                }
            } else {
                for (int i = fromPosition; i > toPosition; i--) {
                    Collections.swap(mDatas, i, i - 1);
                }
            }
            adapter.notifyItemMoved(fromPosition, toPosition);//刷新
            return true;
        }

        /**
         * 当item被左右滑动时调用
         * @param viewHolder
         * @param direction
         */
        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();
            mDatas.remove(position);
            adapter.notifyItemRemoved(position);
            Log.e("tag","onSwiped");
        }

        /**
         * 当Item被长按的时候是否可以被拖拽
         * 相当于长按后执行了：mItemTouchHelper.startDrag(holder);//开始拖动
         * @return 返回true：长按item时，就可以实现拖动效果
         *          返回false：长按item事件就失效了，也就不能直接实现拖动效果了
         */
        @Override
        public boolean isLongPressDragEnabled() {
            return true;
        }

        /**
         * 左右滑动时是否可以执行onSwiped()方法
         * @return 返回true:代表左右滑动item时，可以删除
         */
        @Override
        public boolean isItemViewSwipeEnabled() {
            //Log.e("tag","滑动了--isItemViewSwipeEnabled");
            return true;
        }

        /**
         * 开始拖拽时调用
         * @param viewHolder
         * @param actionState
         */
        @Override
        public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
            if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                viewHolder.itemView.setBackgroundColor(Color.LTGRAY);//为拖拽的item设置背景颜色
            }
            super.onSelectedChanged(viewHolder, actionState);
        }

        /**
         * 拖拽结束时调用
         * @param recyclerView
         * @param viewHolder
         */
        @Override
        public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);
            viewHolder.itemView.setBackgroundColor(0);
            Log.e("tag","拖拽结束");
        }
    });
    //与RecyclerView绑定



















}
